<?php

/**
 * Maikuro Framework
 *
 * LICENSE:
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The license is bundled with this package in the file LICENSE.txt.
 * You may also obtain a copy of the License at this URL:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @copyright  Copyright (c) 2010, Jonathan Hedrén
 * @author     Jonathan Hedrén <jonathan.hedren@gmail.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 */

class HtmlHelper
{
    /**
     * Wraps $data with a tag with optional attributes.
     *
     * @param string $data
     * @param string $tag
     * @param string $class
     * @param string $id
     * @param string $extras
     * @return string
     */
    public static function wrap($data, $tag, $class = null, $id = null, $extras = null)
    {
        $html = '<' . $tag;
        
        if ($class !== null) 
            $html .= ' class="' . $class . '"';
        
        if ($id !== null) 
            $html .= ' id="' . $id . '"';

        if ($extras !== null)
            $html .= ' ' . $extras;

        $html .= '>' . $data . '</' . $tag . '>';

        return $html;
    }

    /**
     * Renders an unordered list from an array.
     *
     * @param array $data
     * @param string $class
     * @param string $id
     * @param string $extras
     * @param string $itemPrefix
     * @return void
     */
    public static function renderUL(
        array $data = null,
        $class = null,
        $id = null,
        $extras = null,
        $itemPrefix = null
        )
    {
        if ($data === null) {
            return;
        }

        $html = '<ul';
        if ($class !== null)
            $html .= ' class="' . $class . '"';

        if ($id !== null)
            $html .= ' id="' . $id . '"';

        if ($extras !== null)
            $html .= $extras;

        $html .= ">\n";
        
        foreach ($data as $item) {
            $html .= '<li>' . $itemPrefix . $item . "</li>\n";
        }
        
        $html .= '</ul>';
        echo $html;
    }
}